package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.VisitToDAO;
import com.xinghou.platform.model.main.VisitTo;

/**
 * 
 * @author wxtdy
 *
 */
@Service
public class VisitToDAOImpl {/*
	@Resource
	private JdbcTemplate jdbcTemplate;

	public VisitTo getVisitToById(Integer Id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Create_time, Update_time FROM visit_to WHERE Id = ?";
		VisitTo Visitto = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new VisitToMapper());
		CustomerContextHolder.clearCustomerType();
		return Visitto;
	}

	public int updateVisitTo(VisitTo visitTo) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE visit_to SET From_user_id=?,To_user_id=?,Create_time=?, Update_time=? WHERE Id=?");
		Object[] params = {visitTo.getFromUserId(),visitTo.getToUserId(),visitTo.getCreateTime(),visitTo.getUpdateTime(), visitTo.getId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	public int saveVisitTo(VisitTo visitTo) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("INSERT INTO visit_to (From_user_id,To_user_id,Create_time, Update_time) VALUES (?,?,?,?);");
		Object[] params = {visitTo.getFromUserId(),visitTo.getToUserId(),visitTo.getCreateTime(),visitTo.getUpdateTime()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	public List<VisitTo> getVisitToByToUserId(Integer toUserId,Long time,int rowNum) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Create_time, Update_time FROM visit_to WHERE To_user_id=? AND Update_time<? ORDER BY Update_time DESC LIMIT ?";
		List<VisitTo> list = jdbcTemplate.query(sql, new Object[] {toUserId,time,rowNum}, new VisitToMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	private static final class VisitToMapper implements RowMapper<VisitTo> {
		public VisitTo mapRow(ResultSet rs, int rowNum) throws SQLException {
			VisitTo visitTo = new VisitTo();
			visitTo.setId(rs.getInt("Id"));
			visitTo.setFromUserId(rs.getInt("From_user_id"));
			visitTo.setToUserId(rs.getInt("To_user_id"));
			visitTo.setCreateTime(rs.getTimestamp("Create_time"));
			visitTo.setUpdateTime(rs.getTimestamp("Update_time"));
			return visitTo;
		}
	}

	public VisitTo getVisitToByTowId(int toUserId, int fromUserId)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Create_time, Update_time FROM visit_to WHERE To_user_id = ? AND From_user_id=? LIMIT 1";
		VisitTo visitTo = jdbcTemplate.queryForObject(sql, new Object[] { fromUserId, toUserId}, new VisitToMapper());
		//System.out.println(visitTo.getId());
		CustomerContextHolder.clearCustomerType();
		return visitTo;
	}

	public List<VisitTo> getVisitToGroupByToUserId(Integer toUserId,Long time,int rowNum) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT * FROM(SELECT Id, From_user_id, To_user_id, Create_time, Update_time FROM visit_to WHERE To_user_id=? ORDER BY Update_time DESC) t where t.Update_time<?  GROUP BY t.From_user_id ORDER BY t.Update_time DESC LIMIT 28";
		List<VisitTo> list = jdbcTemplate.query(sql, new Object[] {toUserId,time}, new VisitToMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}*/
}
